package com.servlet;

import com.beans.AdminInfo;
import com.beans.PageInfo;
import com.utils.PageUtil;
import com.utils.StrUtil;
import com.dao.AdminDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/AdminServlet.do")
public class AdminServlet extends HttpServlet {
    AdminDao _adminDao = new AdminDao();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String flag = request.getParameter("flag");
        if ("add".equals(flag)) {
            add(request, response);
        }
        if ("validate".equals(flag)) {
            validate(request, response);
        }
        if ("manage".equals(flag)) {
            manage(request, response);
        }
        if ("lock".equals(flag)) {
            lock(request, response);
        }
        if ("del".equals(flag)) {
            del(request, response);
        }
        if ("delAdmin".equals(flag)) {
            delAdmin(request, response);
        }
        if ("update".equals(flag)){
            update(request,response);
        }
        if("updateAdmin".equals(flag)){
            updateAdmin(request,response);
        }
        if("unLogin".equals(flag)){
            unLogin(request,response);
        }
        if("updatePassword".equals(flag)){
            updatePassword(request,response);
        }
    }
    //修改密码
    private void updatePassword(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String password=request.getParameter("password");
        Integer id=Integer.parseInt(request.getParameter("id"));
        AdminInfo admin=new AdminInfo();
        admin.setPassword(password);
        admin.setId(id);
        int result=_adminDao.updatePassword(admin);
        if(result==1){
            response.getWriter().print("1");
        }
    }
    //退出登录
    private void unLogin(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getSession().invalidate();
        response.getWriter().println("<script> window.top.location.href='login.jsp' </script>");
    }
    //更新用户
    private void updateAdmin(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Integer id=Integer.parseInt(request.getParameter("id"));
        String adminName=request.getParameter("adminName");
        String password=request.getParameter("password");
        String note=request.getParameter("note");
        AdminInfo admin=new AdminInfo();
        admin.setId(id);
        admin.setAdminName(adminName);
        admin.setPassword(password);
        admin.setNote(note);
        admin.setState("1");
        admin.setRoleId(3);
        Integer result=_adminDao.updateAdmin(admin);
        if(result==1){
            response.getWriter().print("1");
        }
    }
    //转向更新页面
    private void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer id= Integer.parseInt(request.getParameter("id"));
        AdminInfo adminInfo=_adminDao.getAdminById(id);
        request.setAttribute("adminInfo",adminInfo);
        request.getRequestDispatcher("/admin/admin_update.jsp").forward(request,response);

    }

    //删除所选
    private void delAdmin(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String[] ids = request.getParameterValues("ck_id");
        _adminDao.delAdmin(ids);
        request.setAttribute("adminManage", "删除成功");
        manage(request, response);
    }

    //删除单个
    private void del(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer id = Integer.parseInt(request.getParameter("id"));
        _adminDao.del(id);
        manage(request, response);
    }

    //解锁  锁定账号
    private void lock(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer id = Integer.parseInt(request.getParameter("id"));
        String item = request.getParameter("item");
        if ("true".equals(item)) {
            _adminDao.lock(id);
        } else {
            _adminDao.unlock(id);
        }
        manage(request, response);
    }

    //用户维护
    private void manage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        int pageIndex = 1;
        int pageSize = 2;
        String pageIndexStr = request.getParameter("pageIndex");
        int rowCount = _adminDao.getAdminCount();
        if (!StrUtil.isNullOrEmpty(pageIndexStr)) {
            pageIndex = Integer.parseInt(pageIndexStr);
        }
        PageInfo page = PageUtil.getPageInfo(pageSize, rowCount, pageIndex);
        List<AdminInfo> adminList = _adminDao.getAdminList(page);
        request.setAttribute("adminList", adminList);
        request.setAttribute("pageInfo", page);

        request.getRequestDispatcher("/admin/admin_manage.jsp").forward(request, response);

    }

    //验证用户名重复
    private void validate(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String username = request.getParameter("username");
        response.setContentType("text/html;charset=utf-8");
        List<AdminInfo> adminList = _adminDao.getAdminName();
        for (AdminInfo list : adminList) {
            if (list.getAdminName().equals(username)) {
                response.getWriter().print("-1");
            }
        }
    }

    //添加用户
    private void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String adminName = request.getParameter("adminName");
        String password = request.getParameter("password");
        String note = request.getParameter("note");

        AdminInfo admin = new AdminInfo();
        admin.setAdminName(adminName);
        admin.setPassword(password);
        admin.setNote(note);
        admin.setState("1");  //用户默认的状态是可用
        //admin.setEditDate(editDate);  由于最后更新日期在数据库中是时间戳,所以不用传值
        admin.setRoleId(0);
        int result = _adminDao.addAdmin(admin);
        if (result == 1) {
            request.setAttribute("msg", "用户添加成功");
            request.getRequestDispatcher("/admin/admin_add.jsp").forward(request, response);
        }
    }

}
